package com.example.safeassistan.home.fragment;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseVPFragment;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.home.activity.RiskPointDetail;
import com.example.safeassistan.home.adapter.RiskDetailRiskFragmentAdapter;
import com.example.safeassistan.home.adapter.RiskDetailRiskFragmentPAdapter;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.risk.model.RiskPointDetailModel;
import com.example.safeassistan.risk.model.RiskPointDetailRisk;
import com.example.safeassistan.utile.JsonUtils;
import com.example.safeassistan.widget.ViewPagerForScrollView;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.internal.$Gson$Preconditions;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class RiskDetailRiskFragment extends BaseVPFragment {
    private Context context;
    private View view;
    private RecyclerView rcView;
    private LinearLayout llNodata;
    private String riskpointId="";

    private RiskDetailRiskFragmentPAdapter adapter;
    private ViewPagerForScrollView pager;


    public RiskDetailRiskFragment(ViewPagerForScrollView pager,String riskpointId) {
        this.pager = pager;
        this.riskpointId=riskpointId;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_riskdetail_risk, container, false);
            pager.setObjectForPosition(view,3);
            context = getActivity();
            initView();
        }
        return view;
    }

    public void initView(){
        rcView=view.findViewById(R.id.rcView);
        llNodata=view.findViewById(R.id.llNodata);

    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        if (isVisible){
            getData();
        }
    }

    @Override
    protected void onFragmentFirstVisible() { }

    public void getData(){
        new HttpRequest(context).doGet(UrlConstants.getDangerRiskList+riskpointId, null, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    int  code = jsonObject.getInt("code");
                    if (code==200) {
                        Object obj = JsonUtils.fromJson(result.toString(), RiskPointDetailRisk.class);
                        if (obj instanceof RiskPointDetailRisk) {
                            RiskPointDetailRisk  riskPointDetailRisk=(RiskPointDetailRisk) obj;
                            if (riskPointDetailRisk.getResult().size()>0){
                                llNodata.setVisibility(View.GONE);
                                LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false){
                                    @Override
                                    public boolean canScrollVertically() {
                                        return false;
                                    }
                                };
                                rcView.setLayoutManager(linearLayoutManager);
                                adapter=new RiskDetailRiskFragmentPAdapter(riskPointDetailRisk.getResult());
                                rcView.setAdapter(adapter);
                                adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                                    @Override
                                    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                        switch (view.getId()){
                                            case R.id.tvChange:
                                                if (riskPointDetailRisk.getResult().get(position).isExpand()){
                                                    //已经是展开的
                                                    riskPointDetailRisk.getResult().get(position).setExpand(false);
                                                }else {
                                                    riskPointDetailRisk.getResult().get(position).setExpand(true);
                                                }
                                                adapter.notifyDataSetChanged();
                                                break;
                                        }
                                    }
                                });
                            }else {
                                llNodata.setVisibility(View.VISIBLE);
                            }
                        }
                    }else {
                        Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
            }
        });
    }
}
